package com.atguigu.classroom.live.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.classroom.exception.ClassroomException;
import com.atguigu.classroom.live.mapper.LiveCourseGoodsMapper;
import com.atguigu.classroom.live.mapper.LiveCourseMapper;
import com.atguigu.classroom.live.mtcloud.CommonResult;
import com.atguigu.classroom.live.mtcloud.MTCloud;
import com.atguigu.classroom.live.service.LiveCourseGoodsService;
import com.atguigu.classroom.model.live.LiveCourse;
import com.atguigu.classroom.model.live.LiveCourseConfig;
import com.atguigu.classroom.live.mapper.LiveCourseConfigMapper;
import com.atguigu.classroom.live.service.LiveCourseConfigService;
import com.atguigu.classroom.model.live.LiveCourseGoods;
import com.atguigu.classroom.vo.live.LiveCourseConfigVo;
import com.atguigu.classroom.vo.live.LiveCourseGoodsView;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 直播课程配置表 服务实现类
 * </p>
 * @author atguigu
 * @since 2022-07-26
 */
@Service
public class LiveCourseConfigServiceImpl extends ServiceImpl<LiveCourseConfigMapper, LiveCourseConfig> implements LiveCourseConfigService {
    @Autowired
    private LiveCourseConfigMapper liveCourseConfigMapper;
    @Autowired
    private LiveCourseGoodsMapper liveCourseGoodsMapper;
    @Autowired
    private LiveCourseGoodsService liveCourseGoodsService;
    @Autowired
    private LiveCourseMapper liveCourseMapper;
    @Autowired
    private MTCloud mtCloud;
    @Override
    public LiveCourseConfigVo getLiveCourseConfigByLiveCourseId(Long id) {
        LambdaQueryWrapper<LiveCourseConfig> configQueryWrapper=new LambdaQueryWrapper<>();
        configQueryWrapper.eq(LiveCourseConfig::getLiveCourseId,id);
        LiveCourseConfig liveCourseConfig = liveCourseConfigMapper.selectOne(configQueryWrapper);
        //sql-1:查询配置基本信息
        if(liveCourseConfig==null){
            return null;
        }
        LambdaQueryWrapper<LiveCourseGoods> goodsQueryWrapper=new LambdaQueryWrapper<>();
        goodsQueryWrapper.eq(LiveCourseGoods::getLiveCourseId,id);
        List<LiveCourseGoods> liveCourseGoodsList = liveCourseGoodsMapper.selectList(goodsQueryWrapper);
        //sql-2:查询对应商品信息
        LiveCourseConfigVo liveCourseConfigVo=new LiveCourseConfigVo();
        BeanUtils.copyProperties(liveCourseConfig,liveCourseConfigVo);
        liveCourseConfigVo.setLiveCourseGoodsList(liveCourseGoodsList);
        return liveCourseConfigVo;
    }

    @Override
    @Transactional
    public void updateOrAddConfig(LiveCourseConfigVo liveCourseConfigVo) {

        LiveCourseConfig liveCourseConfig=new LiveCourseConfig();
        BeanUtils.copyProperties(liveCourseConfigVo,liveCourseConfig);
        //sql-1:修改或者添加直播配置表的数据
        if(liveCourseConfig.getId()!=null){
            liveCourseConfigMapper.updateById(liveCourseConfig);
            //sql-2:修改直播商品表的数据--1：先删除原有的数据
            LambdaUpdateWrapper<LiveCourseGoods> goodsUpdateWrapper=new LambdaUpdateWrapper<>();
            goodsUpdateWrapper.eq(LiveCourseGoods::getLiveCourseId,liveCourseConfig.getLiveCourseId());
            liveCourseGoodsMapper.delete(goodsUpdateWrapper);
        }else {
            liveCourseConfigMapper.insert(liveCourseConfig);
        }
        //sql-2|3:修改直播商品表的数据--2：添加新数据
        if(liveCourseConfigVo.getLiveCourseGoodsList().size()!=0){
            liveCourseGoodsService.saveBatch(liveCourseConfigVo.getLiveCourseGoodsList());
        }
        //修改欢拓云直播平台的数据
        this.updateTalkFunConfig(liveCourseConfigVo);
    }
    //修改欢拓云直播平台的数据方法
    private void updateTalkFunConfig(LiveCourseConfigVo liveCourseConfigVo) {
        //sql-1:查询直播课程基本信息
        LiveCourse liveCourse = liveCourseMapper.selectById(liveCourseConfigVo.getLiveCourseId());
        //参数设置
        HashMap<Object,Object> options = new HashMap<>();
        //界面模式
        options.put("pageViewMode", liveCourseConfigVo.getPageViewMode());

        JSONObject number = new JSONObject();
        number.put("enable", liveCourseConfigVo.getNumberEnable());
        options.put("number", number.toJSONString());
        //观看人数开关
        number.put("enable", liveCourseConfigVo.getStoreEnable());
        number.put("type", liveCourseConfigVo.getStoreType());
        options.put("store", number.toJSONString());
        //商城列表
        List<LiveCourseGoods> liveCourseGoodsList = liveCourseConfigVo.getLiveCourseGoodsList();
        if(liveCourseGoodsList.size()!=0) {
            List<LiveCourseGoodsView> liveCourseGoodsViewList=liveCourseGoodsList.stream().map(liveCourseGoods -> {
                LiveCourseGoodsView liveCourseGoodsView=new LiveCourseGoodsView();
                BeanUtils.copyProperties(liveCourseGoods, liveCourseGoodsView);
                return liveCourseGoodsView;
            }).collect(Collectors.toList());
            options.put("goodsList", JSON.toJSONString(liveCourseGoodsViewList));

            JSONObject goodsListEdit = new JSONObject();
            goodsListEdit.put("status", "0");
            options.put("goodsListEdit ", goodsListEdit.toJSONString());
        }
        try {
            String res = mtCloud.courseUpdateLifeConfig(liveCourse.getCourseId().toString(), options);
            CommonResult<JSONObject> commonResult = JSON.parseObject(res, CommonResult.class);
            if(Integer.parseInt(commonResult.getCode()) != MTCloud.CODE_SUCCESS) {
                throw new ClassroomException(20001,"欢拓云修改配置信息失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ClassroomException(20001,"欢拓云修改配置信息失败");
        }
    }
}